package org.jsping.music;
import java.util.HashMap;
import java.util.Map;

import org.jsping.entity.Music;

/**
 * 这个类由单例模式实现 主要是储存歌曲的数据，做的一个缓存
 * @author ping
 */
public class CacheMusic {
	
	private static CacheMusic cacheMusic;
	public Map<String, Music> musiclist = new HashMap<String, Music>();
	
	private CacheMusic() {}
	
	public static CacheMusic getInstance() {
		if(cacheMusic == null) {
			synchronized (CacheMusic.class) {
				if(cacheMusic == null) {
					cacheMusic = new CacheMusic();
				}
			}
		}
		return cacheMusic;
	}
	
	//获取歌曲
	public Map<String, Music> getMusic(int page) {
		Map<String, Music> pageMusic = new HashMap<String, Music>();
		int i = 1;
		int maxPage = 12*page;
		int minPage = maxPage-12;
		//第一页page==1；最大就是page*12；最小就是page*12-12
		for(String key : musiclist.keySet()) {
			if(i>minPage&&i<=maxPage) {				
				pageMusic.put(key, musiclist.get(key));
			}
			i++;
		}
		return pageMusic;
	}
	
	//储存歌曲
	public void storageMusic(Map<String, Music> m) {
		this.musiclist.clear();
		this.musiclist = m;
	}
	
}
